/***************************************************************************
 *                                                                         *
 * Copyright (c) 2007 - 2009 Nuvoton Technology Corp. All rights reserved.*
 *                                                                         *
 ***************************************************************************/

/****************************************************************************
 * 
 * FILENAME
 *     NUC900_VPOST.c
 *
 * VERSION
 *     0.1 
 *
 * DESCRIPTION
 *
 *
 *
 *
 * DATA STRUCTURES
 *     None
 *
 * FUNCTIONS
 *
 *
 *     
 * HISTORY
 *     2005.03.16		Created by Shih-Jen Lu
 *
 *
 * REMARK
 *     None
 *
 *
 **************************************************************************/
#include "wblib.h"
#include "NUC900_VPOST_Regs.h"
#include "NUC900_VPOST.h"

#include <stdio.h>

VOID * g_VAFrameBuf;
VOID * g_VAOrigFrameBuf;

INT vpostLCMInit(PLCDFORMATEX plcdformatex)
{
    
	//outpw(0xb0000220,0x1);//SW reset LCD

#ifdef HAVE_OPT_NTSC
    return vpostLCMInit_OPT_NTSC(plcdformatex);
#endif

#ifdef HAVE_OPT_NTSC_NONINT_PATCH
	return vpostLCMInit_OPT_NTSC_NONINT_PATCH(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif		

#ifdef HAVE_OPT_EPSON
	return vpostLCMInit_OPT_EPSON(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif		

#ifdef HAVE_OPT_UNIPAC
	return vpostLCMInit_OPT_UNIPAC(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif		

#ifdef HAVE_SAMSUNG_80_16BIT
	return vpostLCMInit_SAMSUNG_80_16BIT(plcdformatex);
#endif		

#ifdef HAVE_SAMSUNG_80_18BIT
	return vpostLCMInit_SAMSUNG_80_18BIT(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif		

#ifdef HAVE_SAMSUNG_80_9BIT
	return vpostLCMInit_SAMSUNG_80_9BIT(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif

#ifdef HAVE_TOPPOLY_80_16BIT
	return vpostLCMInit_TOPPOLY_80_16BIT(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif

#ifdef HAVE_TOPPOLY_80_8BIT
	return vpostLCMInit_TOPPOLY_80_8BIT(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif

#ifdef HAVE_TOPPOLY_80_18BIT
	return vpostLCMInit_TOPPOLY_80_18BIT(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif

#ifdef HAVE_TOPPOLY_80_9BIT
	return vpostLCMInit_TOPPOLY_80_9BIT(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif

#ifdef HAVE_TOPPOLY_68_16BIT
	return vpostLCMInit_TOPPOLY_68_16BIT(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif

#ifdef HAVE_TOPPOLY_68_8BIT
	return vpostLCMInit_TOPPOLY_68_8BIT(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif

#ifdef HAVE_TOPPOLY_68_18BIT
	return vpostLCMInit_TOPPOLY_68_18BIT(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif

#ifdef HAVE_TOPPOLY_68_9BIT
	return vpostLCMInit_TOPPOLY_68_9BIT(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif

#ifdef HAVE_LPL_LB064
	return vpostLCMInit_LPL_LB064(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif

#ifdef HAVE_HS_121PX13
	return vpostLCMInit_HS_121PX13(plcdformatex);
#endif

#ifdef HAVE_TOPPOLY_TD035
	return vpostLCMInit_TOPPOLY_TD035(plcdformatex);
#endif

#ifdef HAVE_AUO_A025DL02
	return vpostLCMInit_AUO_A025DL02(fnCallBack,ucVASrcType,ucVADisMode,usVAPicWidth,ucROT90);
#endif

#ifdef HAVE_HIMAX_80_18BIT
	return vpostLCMInit_HIMAX_80_18BIT(plcdformatex);
#endif

#ifdef HAVE_TOPPOLY_TD035TTEA3
	return vpostLCMInit_TOPPOLY_TD035TTEA3(plcdformatex);
#endif

#ifdef HAVE_AUO_G104SN03
	return vpostLCMInit_AUO_G104SN03(plcdformatex);
#endif

#ifdef HAVE_AUO_A035QN02
	return vpostLCMInit_AUO_A035QN02(plcdformatex);
#endif

#ifdef HAVE_DATAIMAGE_FG0700A0DSSWAGT1
	return vpostLCMInit_DATAIMAGE_FG0700A0DSSWAGT1(plcdformatex);
#endif
	return ERR_NO_DEVICE;
}

INT vpostLCMDeinit()
{
    INT nRetVal=0;

#ifdef HAVE_TOPPOLY_TD035
	nRetVal = vpostLCMDeinit_TOPPOLY_TD035();
#endif

#ifdef HAVE_TOPPOLY_TD035TTEA3
	nRetVal = vpostLCMDeinit_TOPPOLY_TD035TTEA3();
#endif
    outpw(REG_CLKEN,inpw(REG_CLKEN) &~ 0x1);
    
    return nRetVal;
}

VOID* vpostGetFrameBuffer()
{
    return g_VAFrameBuf;
}

INT vpostOSDInit(POSDFORMATEX pOSDformatex)
{
#ifdef HAVE_OPT_NTSC
	return	vpostOSDInit_OPT_NTSC(pOSDformatex);
#endif

#ifdef HAVE_OPT_NTSC_NONINT_PATCH
	if (_eDisplayDev == VA_DEV_OPT_NTSC_NONINT_PATCH )
		vpostOSDInit_OPT_NTSC_NONINT_PATCH(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif		

#ifdef HAVE_OPT_EPSON
	if (_eDisplayDev == VA_DEV_OPT_EPSON)
		vpostOSDInit_OPT_EPSON(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif		

#ifdef HAVE_OPT_UNIPAC
	if (_eDisplayDev == VA_DEV_OPT_UNIPAC)
		vpostOSDInit_OPT_UNIPAC(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif		

#ifdef HAVE_SAMSUNG_80_16BIT
    return vpostOSDInit_SAMSUNG_80_16BIT(pOSDformatex);
#endif		

#ifdef HAVE_SAMSUNG_80_18BIT
	if (_eDisplayDev == VA_DEV_SAMSUNG_80_18BIT)
		return vpostOSDInit_SAMSUNG_80_18BIT(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif		

#ifdef HAVE_SAMSUNG_80_9BIT
	if (_eDisplayDev == VA_DEV_SAMSUNG_80_9BIT)
		vpostOSDInit_SAMSUNG_80_9BIT(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif

#ifdef HAVE_TOPPOLY_80_16BIT
	if (_eDisplayDev == VA_DEV_TOPPOLY_80_16BIT)
		vpostOSDInit_TOPPOLY_80_16BIT(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif

#ifdef HAVE_TOPPOLY_80_8BIT
	if (_eDisplayDev == VA_DEV_TOPPOLY_80_8BIT)
		vpostOSDInit_TOPPOLY_80_8BIT(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif

#ifdef HAVE_TOPPOLY_80_18BIT
	if (_eDisplayDev == VA_DEV_TOPPOLY_80_18BIT)
		vpostOSDInit_TOPPOLY_80_18BIT(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif

#ifdef HAVE_TOPPOLY_80_9BIT
	if (_eDisplayDev == VA_DEV_TOPPOLY_80_9BIT)
		vpostOSDInit_TOPPOLY_80_9BIT(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif

#ifdef HAVE_TOPPOLY_68_16BIT
	if (_eDisplayDev == VA_DEV_TOPPOLY_68_16BIT)
		vpostOSDInit_TOPPOLY_68_16BIT(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif

#ifdef HAVE_TOPPOLY_68_8BIT
	if (_eDisplayDev == VA_DEV_TOPPOLY_68_8BIT)
		vpostOSDInit_TOPPOLY_68_8BIT(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif

#ifdef HAVE_TOPPOLY_68_18BIT
	if (_eDisplayDev == VA_DEV_TOPPOLY_68_18BIT)
		vpostOSDInit_TOPPOLY_68_18BIT(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif

#ifdef HAVE_TOPPOLY_68_9BIT
	if (_eDisplayDev == VA_DEV_TOPPOLY_68_9BIT)
		vpostOSDInit_TOPPOLY_68_9BIT(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif

#ifdef HAVE_LPL_LB064
	if (_eDisplayDev == VA_DEV_LPL_LB064)
		vpostOSDInit_LPL_LB064(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif

#ifdef HAVE_HS_121PX13
	return	vpostOSDInit_HS_121PX13(pOSDformatex);
#endif

#ifdef HAVE_TOPPOLY_TD035
    return vpostOSDInit_TOPPOLY_TD035(pOSDformatex);
#endif

#ifdef HAVE_AUO_A025DL02
	if (_eDisplayDev == VA_DEV_AUO_A025DL02)
		vpostOSDInit_AUO_A025DL02(ucOSDSrcType,usxstart,usystart,usxend,usyend,usOSDPicWidth);
#endif

#ifdef HAVE_HIMAX_80_18BIT
	return	vpostOSDInit_HIMAX_80_18BIT(pOSDformatex);
#endif

#ifdef HAVE_TOPPOLY_TD035TTEA3
	return vpostOSDInit_TOPPOLY_TD035TTEA3(pOSDformatex);
#endif

#ifdef HAVE_AUO_G104SN03
	return	vpostOSDInit_AUO_G104SN03(pOSDformatex);
#endif

#ifdef HAVE_AUO_A035QN02
	return vpostOSDInit_AUO_A035QN02(pOSDformatex);
#endif

#ifdef HAVE_DATAIMAGE_FG0700A0DSSWAGT1
	return vpostOSDInit_DATAIMAGE_FG0700A0DSSWAGT1(pOSDformatex);
#endif
	return ERR_NO_DEVICE;
	
	
	
}
INT vpostOSDDeinit(VOID)
{
     outpw(REG_LCM_DCCS,inpw(REG_LCM_DCCS) & ~VPOSTB_OSD_EN);//OSD disable
     return 0;
}



VOID vpostOSDSetColKey(UINT32 uCKey_Val)
{
    outpw(REG_LCM_OSD_OVERLAY,inpw(REG_LCM_OSD_OVERLAY) & ~(VPOSTB_BLI_ON | VPOSTB_CKEY_ON) );//blinking disable, color-key disable
    outpw(REG_LCM_OSD_OVERLAY,inpw(REG_LCM_OSD_OVERLAY) | VPOSTB_CKEY_ON);//color-key enable
    outpw(REG_LCM_OSD_CKEY,uCKey_Val);
}

VOID vpostOSDSetColMask(UINT32 uCMask_Val)
{
    outpw(REG_LCM_OSD_CMASK,uCMask_Val);
}

VOID vpostOSDSetOverlay(UINT8 ucOSDDisplayMatch,UINT8 ucOSDDisplayUnMatch,UINT32 uCKey,UINT32 uCMask,UINT8 ucOSDSynW)
{
    outpw(REG_LCM_OSD_OVERLAY,inpw(REG_LCM_OSD_OVERLAY)&0xfffffff0);//clear OCR0 and OCR1
    /* match condition */
    if (ucOSDDisplayMatch != 0){
    	outpw(REG_LCM_OSD_OVERLAY,inpw(REG_LCM_OSD_OVERLAY) | (ucOSDDisplayMatch<<2));
    }
    /* unmatch condition */
    if (ucOSDDisplayUnMatch != 0){
    	outpw(REG_LCM_OSD_OVERLAY,inpw(REG_LCM_OSD_OVERLAY) | (ucOSDDisplayUnMatch));
    }
    /* synthesized weight*/
    if (ucOSDDisplayMatch == DISPLAY_SYNTHESIZED || ucOSDDisplayUnMatch == DISPLAY_SYNTHESIZED){
    	outpw(REG_LCM_OSD_OVERLAY,inpw(REG_LCM_OSD_OVERLAY) | (ucOSDSynW<<4));
    }
    vpostOSDSetColKey(uCKey);
    vpostOSDSetColMask(uCMask);
    vpostOSDEnable();
}
void vpostHCInit(UINT32 *uCursorBMPBuff,VA_HCMODE_E ucMode)
{
	int bpp;
	int BlockWidth;
	int bpw=32;
	outpw(REG_LCM_HC_CTRL,inpw(REG_LCM_HC_CTRL) &~0x003f3f00 | (0x00<<8) | (0x00<<16));//set TIP
	if (ucMode == HC_MODE0)
	{
		bpp=2;
		BlockWidth=32;
		outpw(REG_LCM_HC_CTRL,inpw(REG_LCM_HC_CTRL) &~0x7);//set mode 0 32X32X2bpp 4 color
		
	}
	else if (ucMode == HC_MODE1)
	{
		bpp=2;
		BlockWidth=32;
		outpw(REG_LCM_HC_CTRL,inpw(REG_LCM_HC_CTRL) &~0x7 | 0x1);//set mode 1 32X32X2bpp 3 color and 1 transparent
	}
	else if (ucMode == HC_MODE2)
	{
		bpp=2;
		BlockWidth=64;
		outpw(REG_LCM_HC_CTRL,inpw(REG_LCM_HC_CTRL) &~0x7 | 0x2);//set mode 2 64X64X2bpp 4 color
	}
	else if (ucMode == HC_MODE3)
	{
		bpp=2;
		BlockWidth=64;
		outpw(REG_LCM_HC_CTRL,inpw(REG_LCM_HC_CTRL) &~0x7 | 0x3);//set mode 3 64X64X2bpp 3 color and 1 transparent
	}
	else if (ucMode == HC_MODE4)
	{
		bpp=1;
		BlockWidth=128;
		outpw(REG_LCM_HC_CTRL,inpw(REG_LCM_HC_CTRL) &~0x7 | 0x4);//set mode 4 128X128X1bpp 2 color
	}
	else if (ucMode == HC_MODE5)
	{
		bpp=1;
		BlockWidth=128;
		outpw(REG_LCM_HC_CTRL,inpw(REG_LCM_HC_CTRL) &~0x7 | 0x5);//set mode 5 128X128X1bpp 1 color and 1 transparent
	}
	
	outpw(REG_LCM_HC_WBCTRL,((bpp*BlockWidth/bpw)<<16) | (bpp*BlockWidth/bpw) );
	outpw(REG_LCM_HC_BADDR,(UINT32)uCursorBMPBuff);
	outpw(REG_LCM_HC_COLOR0, 0x00ff0000);       // RED color
    outpw(REG_LCM_HC_COLOR1, 0x0000ff00);       // GREEN color                
    outpw(REG_LCM_HC_COLOR2, 0x000000ff);       // BLUE color                                
    outpw(REG_LCM_HC_COLOR3, 0x00ffff00);       // YELLOW color
    outpw(REG_LCM_DCCS,inpw(REG_LCM_DCCS) | VPOSTB_HC_EN);
}
void vpostHCPosCtrl(UINT32 uCursorX, UINT32 uCursorY)
{
	
	outpw(REG_LCM_HC_POS, (uCursorY <<16) | uCursorX);//set Cursor position
	
}

void vpostDeInit(void)
{
    outpw(REG_LCM_DCCS,inpw(REG_LCM_DCCS) & ~VPOSTB_HC_EN);
}

VOID vpostOSDSetBlinking(UINT8 ucOSD_Blink_Vcnt)
{
    outpw(REG_LCM_OSD_OVERLAY,inpw(REG_LCM_OSD_OVERLAY) & ~(VPOSTB_BLI_ON | VPOSTB_CKEY_ON));//blinking disable, color-key disable
    outpw(REG_LCM_OSD_OVERLAY,inpw(REG_LCM_OSD_OVERLAY) | VPOSTB_BLI_ON);
    outpw(REG_LCM_OSD_OVERLAY,inpw(REG_LCM_OSD_OVERLAY) | ((UINT32)(ucOSD_Blink_Vcnt)<<16));
}
VOID vpostOSDDisableBlinking()
{
    outpw(REG_LCM_OSD_OVERLAY,inpw(REG_LCM_OSD_OVERLAY) &~ VPOSTB_BLI_ON);
}


VOID vpostOTFEnable(UINT OTFBufDepth)
{
	if (OTFBufDepth==0)
		outpw(REG_LCM_VA_FBCTRL,inpw(REG_LCM_VA_FBCTRL) & ~(7<<12) | VPOSTB_FLY_EN);//clear setting and enable OTF
	else	
		outpw(REG_LCM_VA_FBCTRL,inpw(REG_LCM_VA_FBCTRL) | OTFBufDepth | VPOSTB_FLY_EN);
}

